﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace CIRDiagnoseDevice.Control
{
    class Control_SerialPort
    {
        public  SerialPort serialport=new SerialPort();
        public  Control_SerialPort(SerialPort SerialPort)
        {
              
             this.serialport=SerialPort;  
        }
        public Control_SerialPort()
        {  
        }
       /// <summary>
       /// 打开与接口单元通信串口
       /// </summary>
       /// <param name="OSP"></param> 
        public void OpenSerialPort()
        {
            this.serialport .DataBits = 8;
            this.serialport.StopBits = StopBits.One;
            if (!this.serialport.IsOpen)
            {
                try
                {
                    this.serialport.Open();
                }
                catch
                {

                }
            }
        }
        /// <summary>
        /// 关闭接口
        /// </summary>
        public void closeSerialPort()
        {
            if (this.serialport.IsOpen)
            {
                try
                {
                    this.serialport.Close();
                }
                catch
                {

                }
            }

        }
        /// <summary>
        /// 向此串口写入字符串命令
        /// </summary>
        /// <param name="commandStr"></param>
        public void SendStrMessage(string commandStr)
        {
            if (this.serialport.IsOpen)
            {
                byte[] sendByte = System.Text.ASCIIEncoding.Default.GetBytes(commandStr);
                this.serialport.Write(sendByte, 0, sendByte.Length);
            }
        }

    }
}
